﻿using UnityEngine;
using System.Collections;

public class MainSceneManager : LTEventListenerC {
	public GameObject selectSingleMapWindowObject;
	public GameObject tempSelectMultiMapWindowObject;
	public GameObject singleMapSelectedMark;
	//public GameObject topBarObject;
	public GameObject myCharObject;
	public TempCharacterSelectWindow tempCharacterListWindow;	

	public Animator cardMenuNaviButton;
	
	// Use this for initialization
	void Start () {
		LoadingWindow.window.HideLoading();
        //StartCoroutine("tempShowDailyAttendanceCheckPopup");    // 출석체크 팝업 띄우기

        if (MyInfo.Instance.isLevelUp)
        {
            MyInfo.Instance.isLevelUp = false;
            PopupManager.PopupPush(PopupKind.Popup_Levelup);
        }

        /*
		int pawn = MyInfo.Instance.pawnId;
		if( pawn == 1 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(5f, 40f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(280f, 470f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 2;
		}
		else if( pawn == 2 )
		{
			
			(myCharObject.collider as BoxCollider).center = new Vector3(0f, -20f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(180f, 370f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 11;
		}
		else if( pawn == 3 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(0f, -20f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(180f, 370f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 12;
		}
		else if( pawn == 4 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(-20f, 10f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(300f, 390f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 22;
		}
		else if( pawn == 5 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(-20f, 10f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(300f, 390f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 22;
		}

        if (CSceneManager.NeedToDisconnect)
        {
            CNetworkManager.Instance.Disconnect();
        }
        */
	}

	public void SetCharacterObject(int selectedCharacterId)
	{
		int pawn = MyInfo.Instance.pawnId = selectedCharacterId;
		if( pawn == 1 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(5f, 40f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(280f, 470f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 2;
		}
		else if( pawn == 2 )
		{
			
			(myCharObject.collider as BoxCollider).center = new Vector3(0f, -20f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(180f, 370f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 11;
		}
		else if( pawn == 3 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(0f, -20f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(180f, 370f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 12;
		}
		else if( pawn == 4 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(-20f, 10f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(300f, 390f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 22;
		}
		else if( pawn == 5 )
		{
			(myCharObject.collider as BoxCollider).center = new Vector3(-20f, 10f, 21f);
			(myCharObject.collider as BoxCollider).size = new Vector3(300f, 390f, 0f);
			myCharObject.GetComponent<tk2dSprite>().spriteId = 22;
		}

		//topBarObject.GetComponent<TopInfoBar>().UpdateInfos();
	}

    IEnumerator tempShowDailyAttendanceCheckPopup()
    {
        yield return new WaitForSeconds(0.5f);
        PopupManager.PopupPush(PopupKind.Popup_DailyAttendanceCheck);
    }  
	
	// Update is called once per frame
	void Update () {
	
	}

	bool isOpendTempCharacterListWindow = false;
	void OnClickSetCurrentCharacterButton()
	{
		tempCharacterListWindow.manager = this;
		if( !isOpendTempCharacterListWindow )
		{
			isOpendTempCharacterListWindow = true;
			tempCharacterListWindow.gameObject.SetActive(true);
		}
		else
		{
			isOpendTempCharacterListWindow = false;
			tempCharacterListWindow.gameObject.SetActive(false);
		}
	}
	
	void OnClickMultiGameButton()
	{
		CSceneManager.isSingleGame = false;
		CSceneManager.manager.GoToPvPCreateEnterRoomScene();
	}
	
	void OnClickSingleGameButton()
	{     
        //if (selectSingleMapWindowObject.activeSelf)
        //    selectSingleMapWindowObject.SetActive(false);
        //else
        //    selectSingleMapWindowObject.SetActive(true);
        PVEChapterSelectMenu.OpenMenu();
	}
	
	int prevSelectedSingleMapIndex = -1;
	public static int selectedSingleMapIndex = -1;
	public static string selectedSingleMapName = "";
	string[] mapName = new string[5]{"M1_1", "pvp2", "M1_3", "M1_4", "M1_5"};
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);
		
		//singleMapSelected!
		if( eventID < 5 )
		{
			prevSelectedSingleMapIndex = selectedSingleMapIndex;
			selectedSingleMapIndex = eventID;
			selectedSingleMapName = mapName[selectedSingleMapIndex];
			if( prevSelectedSingleMapIndex == selectedSingleMapIndex )
			{
				if( singleMapSelectedMark.activeSelf )
				{
					singleMapSelectedMark.SetActive(false);
					selectSingleMapWindowObject.SetActive(false);
					LoadingWindow.window.ShowShortLoading();
					CSceneManager.isSingleGame = true;
					CSceneManager.manager.GoToGameReadyScene();
					prevSelectedSingleMapIndex = selectedSingleMapIndex = -1;
				}
			}
			else
			{
				singleMapSelectedMark.SetActive(true);
				singleMapSelectedMark.transform.localPosition = new Vector3(-480+(eventID*240), singleMapSelectedMark.transform.localPosition.y, singleMapSelectedMark.transform.localPosition.z);
			}
		}

		if( eventID > 999 )
		{
			if( eventID == 1000 )
				CSceneManager.m_mapName = "map_pvp1";
			else if( eventID == 1001 )
				CSceneManager.m_mapName = "map_pvp2";
			else if( eventID == 1002 )
				CSceneManager.m_mapName = "map_pvp3";
			CSceneManager.manager.GoToGameReadyScene();
		}
	}
	
	void OnClickBackButton()
	{
	}
	
	
	
	bool isCharClicked = false;
	void OnClickCharacter()
	{
		if( isCharClicked == false )
		{
			isCharClicked = true;
			int pawn = MyInfo.Instance.pawnId;
			myCharObject.GetComponent<tk2dSpriteAnimator>().Play("0" + pawn +"_idle_s_front");
			StartCoroutine("Co_CharacterAnimStop");		
		}
	}
	
	IEnumerator Co_CharacterAnimStop()
	{
		yield return new WaitForSeconds(2f);
		myCharObject.GetComponent<tk2dSpriteAnimator>().StopAndResetFrame();
		isCharClicked = false;
	}


    void OnMultimapBGButton(GameObject obj)
    {
        tempSelectMultiMapWindowObject.gameObject.SetActive(false);
    }

	bool isClickedCardMenuNaviButton = false;
	void OnClickCardMenuNaviButton()
	{
		if( !isClickedCardMenuNaviButton )
		{
			isClickedCardMenuNaviButton = true;
			cardMenuNaviButton.Play("Btn_Card");
		}
		else
		{
			isClickedCardMenuNaviButton = false;
			cardMenuNaviButton.Play("Btn_CardOff");
		}
	}

	void OnClickedUnitLevelupButton()
	{
		CSceneManager.manager.GoToUnitLevelUpScene();
	}

    void OnEvolutionButton()
    {
        CSceneManager.manager.GoToUnitPowerUpScene();
    }
}
